<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>用户</el-breadcrumb-item>
        <el-breadcrumb-item>付费用户统计</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
       <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
    <export-message :dialogVisible="dialogVisible" ></export-message>
  </div>
</template>

<script>
import { handleExcelCat } from "@/api/exportCommon";
import { common } from "@/api/cat/httpRequest";
import { appStatisticsMemberBuy as baseUrl } from '@/api/cat/baseUrls';
export default {
  data() {
    return {
      dialogVisible: false,
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        border: true,
        menu: false,
        column: [
          {
            label: "日期",
            prop: "stats_date",
            type: "datetime",
          },
          {
            label: "会员总用户数",
            type: "input",
            prop: "member_total",
          },
          {
            label: "月卡会员总数",
            prop: "month_card_member_num",
            type: "input",
          },
          {
            label: "季卡会员总数",
            prop: "season_card_member_num",
            type: "input",
          },
          {
            label: "年卡会员总数",
            prop: "year_card_member_num",
            type: "input",
          },
          {
            label: "当天新增会员数",
            prop: "add_member_num",
            type: "input",
          },
          {
            label: "新增月卡会员数",
            prop: "month_card_buy_num",
            type: "input",
          },
          {
            label: "新增季卡会员数",
            prop: "season_card_buy_num",
            type: "input",
          },
          {
            label: "新增年卡会员数",
            prop: "year_card_bug_num",
            type: "input",
          },
          {
            label: "当天购买书币总用户数",
            prop: "current_coin_bug_user_num",
            type: "input",
          },
          {
            label: "选择日期",
            type: "daterange",
            prop: "daterange",
            valueFormat:'yyyy-MM-dd',
             hide: true,
            search: true,
          },
        ],
      },
    };
  },
  methods: {
    /**数据导出 */
    async handleExcel() {
      this.loading = true;
      this.dialogVisible = true;
      //处理搜索条件
      let params = {...this.$refs.crud.searchForm};
      params.route = "数据/用户/购买会员统计";
      
      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000;
      if(params.daterange.length>0){
        export_condition += " &时间区间："+params.daterange[0].toString() + "~" + params.daterange[1].toString();
        params.startDate = params.daterange[0];
        params.endDate = params.daterange[1];
      }
      params.export_condition = export_condition == "" ? "所有" : export_condition;
      
      //调用导出接口
      let response = await common(
        baseUrl.exportData,
        { ...params, ...this.default },
        this.page,
        "POST"
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //this.params.orderByList = "stats_date&DESC";
      //发送请求获取数据
      let response = await common(
        `${baseUrl.statisticsAll}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );

      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
       if(params.daterange){
        params.startDate = params.daterange[0]
        params.endDate = params.daterange[1]
       }
      this.params = params;
      this.page.currentPage = 1;
      delete params.daterange
      this.getList();
      if (done) {
        done();
      }
    },
  },
};
</script>